﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Portal.HtmlTool;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;
using NetDon.Utilities;
using NetDon.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;


namespace NetDon.eBoss.Portal.HtmlTool
{
    public partial class TemplateDetailsEdit : NetDonPage
    {
        #region Properties
        protected int Id
        {
            get
            {
                return Parameters.GetInt32("Id");
            }
        }
        protected int AccountId
        {
            get
            {
                return Parameters.GetInt32("AccountId");
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                CancelButton = btnReturn;


                if (Id > 0)
                {
                    ddlHtmlToolCategory.InsertEmptyItem(ListEmptyType.Select);
                    ddlHtmlToolTrademark.InsertEmptyItem(ListEmptyType.Select);

                    LoadAccounts();
                    var productGroups = new ProductgroupCollection();
                    productGroups.GetMulti(ProductgroupFields.Accountid == AccountId & ProductgroupFields.Sourceid == Id);
                    var productGroup = productGroups.FirstOrDefault();
                    if (productGroup != null)
                    {
                      
                        panelCustomInfo.DataSource = productGroup;
                        panelCustomInfo.DataBind();

                        LoadDropDownLists(productGroup);
                        LoadVocabulary();
                    }

                    InitialUI();
                }


            }
        }

        private void InitialUI()
        {
            //is default account
            if (new HtmltoolaccountEntity(AccountId).Isdefault == 1)
            {
                this.btnSaveAll.Visible = true;
            }
            else
            {
                this.btnSaveAll.Visible = false;
            }
        }



        public void ddlHtmlToolCategory_SelectedIndexChanged(object sender, EventArgs e)
        {

            LoadHtmlToolStyles();
            LoadVocabulary();

        }

        public void ddlHtmlToolTrademark_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolTrademark.SelectedValue))
            {
                txtMarket.Text = new HtmltooltrademarksettingEntity(ddlHtmlToolTrademark.SelectedValue.ToInt32().Value).Market;
            }


        }

        private void LoadAccounts()
        {
            var accounts = new HtmltoolaccountCollection();
            accounts.GetMulti(null, -1, new SortExpression(HtmltoolaccountFields.Isdefault | (SortOperator)(int)SortDirection.Descending));
            rptAccount.DataSource = accounts;
            rptAccount.DataBind();

        }


        private void LoadHtmlToolStyles()
        {

            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {

                this.ddlHtmlToolStyle.DataSource = new HtmlToolDataSource().RetrieveHtmlToolStyle(Convert.ToInt32(ddlHtmlToolCategory.SelectedValue));
                this.ddlHtmlToolStyle.DataBind();
                ddlHtmlToolStyle.InsertEmptyItem(ListEmptyType.All);
                this.lblTitleVocabulary.Text = Server.HtmlEncode(GetVocabulary(true));
            }
        }

        private void LoadVocabulary()
        {
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {

                this.lblTitleVocabulary.Text = Server.HtmlEncode(GetVocabulary(true));
                this.labelVocabularyMaterial.Text = Server.HtmlEncode(GetVocabulary(false));
            }
            
        }

        private void LoadDropDownLists(ProductgroupEntity entity)
        {
            if (entity != null)
            {
                LoadeBayColor(entity.Color);
                if (entity.Categoryruleid.HasValue)
                    ddlHtmlToolCategory.SelectedValue = entity.Categoryruleid.Value.ToString();
                //if (entity.Colorid.HasValue)
                //    ddlHtmlToolColor.SelectedValue = entity.Colorid.Value.ToString();
                if (entity.Trademarkid.HasValue)
                    ddlHtmlToolTrademark.SelectedValue = entity.Trademarkid.Value.ToString();
                LoadHtmlToolStyles();
               
                if (entity.Styleid.HasValue)
                    ddlHtmlToolStyle.SelectedValue = entity.Styleid.Value.ToString();
            }
        }

        private void LoadeBayColor(string color)
        {
            var service = new HtmlToolService();
            var entity = service.GetTitleColorSetting(color);
            if (entity != null)
            {
                if (entity.Htmltoolcolorid.HasValue)
                {
                    txteBayColor.Text = new HtmltoolcolorEntity(entity.Htmltoolcolorid.Value).Color;
                }
            }
        }




        private bool Save(int accountId)
        {
            var productGroups = new ProductgroupCollection();
            productGroups.GetMulti(ProductgroupFields.Accountid == accountId & ProductgroupFields.Sourceid == Id);
            var entity = productGroups.FirstOrDefault();
            if (entity == null)
            {
                entity = new ProductgroupEntity();
                entity.Sourceid = Id;
                entity.Accountid = accountId;

            }
            entity.Categoryruleid = ddlHtmlToolCategory.SelectedValue.ToInt32();
            entity.Trademarkid = ddlHtmlToolTrademark.SelectedValue.ToInt32();
            entity.Market = txtMarket.Text.Trim();

            entity.Specifics = txtSpecifics.Text.Trim();
            entity.Retailprice = txtRetailPrice.Text;
            entity.Styleid = ddlHtmlToolStyle.SelectedValue.ToInt32();
            //  entity.Colorid = ddlHtmlToolColor.SelectedValue.ToInt32();
            entity.Material = txtMaterial.Text.Trim();
            entity.Comment = txtComment.Text.Trim();
            entity.Description = txtDescription.Text.Trim();
            entity.Picurl1 = txtPicUrl1.Text.Trim();
            entity.Picurl2 = txtPicUrl2.Text.Trim();
            entity.Picurl3 = txtPicUrl3.Text.Trim();
            entity.Picurl4 = txtPicUrl4.Text.Trim();
            entity.Picurl5 = txtPicUrl5.Text.Trim();
            entity.Picurl6 = txtPicUrl6.Text.Trim();
            entity.Picurl7 = txtPicUrl7.Text.Trim();
            entity.Picurl8 = txtPicUrl8.Text.Trim();
            entity.Picurl9 = txtPicUrl9.Text.Trim();
            entity.Picurl10 = txtPicUrl10.Text.Trim();
            entity.Picurl11 = txtPicUrl11.Text.Trim();
            entity.Picurl12 = txtPicUrl12.Text.Trim();

            entity.Sizetable = txtSizetable.Text.Trim();
            entity.Englishtitle = txtEnglishtitle.Text.Trim();


            entity.Save();
            return true;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (Save(AccountId))
            {
                Success("保存本账号成功!");
                CloseWindow(true);
            }



        }
        protected void btnSaveAll_Click(object sender, EventArgs e)
        {
            try
            {
                var derivedAccounts = new HtmlToolAccountService().GetHtmlToolAccountsBySourceAccount(AccountId);
                foreach (var derivedAccount in derivedAccounts)
                {
                    Save(derivedAccount.Id);
                }
                Success("保存所有账号成功!");
                CloseWindow(true);

            }
            catch (Exception)
            {

                throw;
            }

        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            CloseWindow(true);
        }



        protected string GetImageUrl(object id)
        {
            if (MiscUtilities.IsNullOrEmpty(id)) return string.Empty;

            var baseUrl = System.Configuration.ConfigurationSettings.AppSettings["picThumbnailUrl"];
            return string.Format(baseUrl, id);
        }


        public string GetVocabulary(bool isTitle)
        {
            var vocabulary = string.Empty;
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {
                var collection = new HtmltoolvocabularyCollection();
                collection.GetMulti(HtmltoolvocabularyFields.Categoryruleid == ddlHtmlToolCategory.SelectedValue.ToInt32().Value & HtmltoolvocabularyFields.Sitecode == new HtmltoolaccountEntity(AccountId).Sitecode);
                var vocabularyEntity = collection.FirstOrDefault();
                if (vocabularyEntity != null)
                {


                    if (isTitle)
                    {
                        vocabulary = vocabularyEntity.Titleword;
                    }
                    else
                    {
                        vocabulary= vocabularyEntity.Materialword;

                    }
                    vocabulary = vocabulary.Replace(@"\n", "<Br/>").Replace(@"\r", "<Br/>");
                    vocabulary = vocabulary.Replace(@"\t", "<Br/>");
  
                }
            }
            return vocabulary;
        }
    }

}